{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 单位向量\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的核心目标是计算向量 $a$ 的 **L2 范数（即欧几里得范数）**，然后基于此计算单位向量，并最终验证单位向量与原始长度的关系。数学上，该过程涉及向量的归一化和数乘运算，具体分析如下：\n",
    "\n",
    "### 1. 计算向量 $a$ 的 L2 范数\n",
    "\n",
    "首先，给定二维向量：\n",
    "\n",
    "$$\n",
    "a = \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "L2 范数（或欧几里得范数）定义如下：\n",
    "\n",
    "$$\n",
    "\\|a\\|_2 = \\sqrt{3^2 + 4^2} = \\sqrt{9 + 16} = \\sqrt{25} = 5\n",
    "$$\n",
    "\n",
    "代码中的 `np.linalg.norm(a)` 计算了该范数，并将其存储在变量 `norm_a` 中。\n",
    "\n",
    "### 2. 计算单位向量\n",
    "\n",
    "单位向量的定义是 **原始向量除以其范数**，即：\n",
    "\n",
    "$$\n",
    "\\hat{a} = \\frac{a}{\\|a\\|_2} = \\frac{1}{5} \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix} = \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "代码中 `unit_a = a / norm_a` 实现了这一计算，得到向量 $\\hat{a}$，它的特点是方向与 $a$ 相同，但长度为 1：\n",
    "\n",
    "$$\n",
    "\\|\\hat{a}\\|_2 = \\sqrt{0.6^2 + 0.8^2} = \\sqrt{0.36 + 0.64} = \\sqrt{1} = 1\n",
    "$$\n",
    "\n",
    "### 3. 长度 $\\times$ 方向向量\n",
    "\n",
    "在向量运算中，如果将单位向量 $\\hat{a}$ 乘以原始向量的范数 $\\|a\\|_2$，理论上应该得到原向量：\n",
    "\n",
    "$$\n",
    "\\|a\\|_2 \\cdot \\hat{a} = 5 \\cdot \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix} = \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix} = a\n",
    "$$\n",
    "\n",
    "代码中 `norm_a * unit_a` 计算了这个结果，并验证了单位向量的正确性。\n",
    "\n",
    "### 结论\n",
    "\n",
    "1. `np.linalg.norm(a)` 计算向量 $a$ 的模长，结果为 $5$。\n",
    "2. `a / norm_a` 计算单位向量 $\\hat{a}$，即 **保持方向不变但长度为 1**。\n",
    "3. `norm_a * unit_a` 验证单位向量乘以原范数是否能复原向量 $a$，结果证明计算正确。\n",
    "\n",
    "这段代码展示了 **向量归一化** 的基本数学原理，在机器学习、计算机视觉和物理建模等领域都有重要应用，例如方向向量的计算、余弦相似度、梯度归一化等。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e5f832e-6b83-4973-88a7-52f63fe92b0e",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2889d4f3-5ebd-46aa-885b-99bbf6e744aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd7c49d7-03ab-4b61-9326-535ba2eae184",
   "metadata": {},
   "source": [
    "## 定义向量 a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b20fbb44-18d3-43f5-ae82-a44b37cf266b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([3, 4])\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9633ae00-50e4-4d75-8cd4-7c20150ca580",
   "metadata": {},
   "source": [
    "## 计算向量的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "78a0127a-255d-43b6-8931-68cb32bfaf76",
   "metadata": {},
   "outputs": [],
   "source": [
    "norm_a = np.linalg.norm(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "535e230f-6131-41d1-867e-2ade3ee63eb4",
   "metadata": {},
   "source": [
    "## 计算单位向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6bc32359-2ea2-4312-a760-89c370e868f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.6, 0.8])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unit_a = a / norm_a\n",
    "unit_a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce0a2a1d-8fe1-40f0-9ef0-716fdced62fd",
   "metadata": {},
   "source": [
    "## 长度 $\\times$ 方向向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 4.])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm_a * unit_a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f48c7a73-872b-403c-9d99-86e7ef6635bd",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
